package com.test30_IO流;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
 * 字符编码
 */
public class Demo08_Encoding {

    /**
     * 获取当前系统中的环境属性
     */
    @Test
    public void test1() {
        // System.getProperties().list(System.out);
        Properties properties = System.getProperties();
        for (Object key : properties.keySet()) {
            if (String.valueOf(key).startsWith("file")) {
                System.out.println(key + " = " + properties.get(key));
            }
        }
    }

    /**
     * 使用默认的编码存储中文
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        File file = new File("F:" + File.separator + "muyan" + File.separator + "message.txt");
        OutputStream output = new FileOutputStream(file);
        output.write("李兴华编程训练营：yootk.ke.qq.com".getBytes());
        output.close();
    }

    /**
     * 将输出内容转换编码
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        File file = new File("F:" + File.separator + "muyan" + File.separator + "message.txt");
        OutputStream output = new FileOutputStream(file);
        output.write("李兴华编程训练营：yootk.ke.qq.com".getBytes("ISO-8859-1"));    // 会出现乱码
        output.close();
    }
}
